<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;
include_once("Common.php");
include_once("mysqlha.php");
header("Content-Type:text/html; charset=utf-8");
class PresentController extends Controller {

//提现列表
    public function presentList(){
        $data = D('present')->lists($_POST);
        $bank = M('blind_banks')->where()->select();

        // var_dump($data);die;
        $this->assign('presents',$data);
        $this->assign('banks',$bank);
        $this->display("PresentList.php");
    }

//银行列表
    public function bankList(){
        $data = M('banks')->select();
        $this->assign('banks',$data);
        $this->display("BankList.php");
    }

//审查提现
    public function presentCheck(){
        $str = implode(',',I('post.presents',0));
        $strCheck = I('post.checking',0);
        $para['id']=array('in',$str);

        if($strCheck=='1'){//成功后
            $ary=array('account_state'=>1);
            M('present')->where($para)->save($ary);
            $data=M('present')->where($para)->select();
            foreach($data as $v){
                if($v['sid']){
                    $parashop['shopid']=array('in',$v['sid']);
                  M('shop_pay')->add(['sid'=>$v['sid'], 'active_pay'=>2, 'amount'=>$v['account_money'], 'status'=>1, 'style'=>$v['account_method'] ]);
  
                }elseif($v['uid']){
                    $parauser['userid']=array('in',$v['uid']);
                    M('user_pay')->add(['uid'=>$v['uid'], 'active_pay'=>2, 'amount'=>$v['account_money'], 'status'=>1, 'style'=>$v['account_method'] ]);
                }
            }
        redirect('index.php?c=Present&a=presentList#present_list',0.1,'审查成功...');
        }
        elseif($strCheck=='0'){//失败后
            $this->assign('parashop',$str);
            $this->display('CheckReason.php');
        }
    }

// 提现失败添加理由
    public function addReason(){
        $ids=I('post.ids');
        $textReason=I('post.textReason','');
        $para['id'] = array('in',$ids);
        $ary=array('account_state'=>2,'reason'=>$textReason);
        M('present')->where($para)->save($ary);
        $data=M('present')->where($para)->select();
            foreach($data as $v){
                if($v['sid']){
                    $parashop['shopid']=array('in',$v['sid']);
                  M('shop_pay')->add(['sid'=>$v['sid'], 'active_pay'=>2, 'amount'=>$v['account_money'], 'status'=>2, 'style'=>$v['account_method'] ]);
                  $money=M('shop')->where($parashop)->select()[0]['money'];
                  $money += $v['account_money'];
                  M('shop')->where($parashop)->save(array('money'=>$money));  
                }elseif($v['uid']){
                    $parauser['userid']=array('in',$v['uid']);
                    M('user_pay')->add(['uid'=>$v['uid'], 'active_pay'=>2, 'amount'=>$v['account_money'], 'status'=>2, 'style'=>$v['account_method'] ]);
                  $money=M('user')->where($parauser)->select()[0]['money'];
                  $money += $v['account_money'];
                  M('user')->where($parauser)->save(array('money'=>$money));
                }
            }
        $returnAry = array(
            "code"=>200,
            "navTabId"=>"bank_list",
            "forwardUrl"=>"index.php?c=Index&a=index1#present_list",
            "msg"=>"插入审核理由成功"
        );
        echo json_encode($returnAry);echo "<script>window.location.href='index.php?c=Index&a=index1#present_list';</script>"; 
    }

// 提现详情
    public function presentDetail(){
        $details=M('present')->where(['id'=>I('get.id')])->select()[0];
        $bank = M('banks')->select();
        $accountNick=['w'=>'微信','z'=>'支付宝','y'=>'银行卡'];
        $this->assign('banks',$bank);
        $this->assign('nicks',$accountNick);
        $this->assign('detail',$details); 
        $this->display("presentDetail.php"); 
    }

// 流水详情
    public function moneyDetail(){
        $bank = M('banks')->select();
        $uid=M('present')->where(array('id'=>I('get.id','0')))->select()[0]['uid'];
        $sid=M('present')->where(array('id'=>I('get.id','0')))->select()[0]['sid'];
        $realname=M('present')->where(array('id'=>I('get.id','0')))->select()[0]['realname'];
        if($uid){
          $users=M('user_pay')->where(array('uid'=>$uid))->select();
          $this->assign('datas',$users);  
        }
        if($sid){
            $shops=M('shop_pay')->where(array('sid'=>$sid))->select();
            $this->assign('datas',$shops);
        }
        $this->assign('banks',$bank);
        $this->assign('realname',$realname);
        $this->display("moneyDetail.php");
    }

    public function addOne(){
       $this->display("addBank.php"); 
    }

    public function addBank(){
        $Name = I('post.bankName','');
        $data['bank_name'] = $Name;
          if(M('banks')->add($data)){
            $returnAry = array(
                "code"=>200,
                "callbackType"=>"closeCurrent",
                "navTabId"=>"bank_list",
                "forwardUrl"=>"index.php?c=Present&a=bankList",
                "msg"=>"插入银行卡成功"
            );
            echo json_encode($returnAry);
            exit(); 
          }   
    }

    public function scanManager(){
        $Name = I('post.bankName','');
          if(M('banks')->add($ary)){
            $returnAry = array(
                "code"=>200,
                "callbackType"=>"closeCurrent",
                "navTabId"=>"bank_list",
                "forwardUrl"=>"index.php?c=Present&a=bankList",
                "msg"=>"插入银行卡成功"
            );
            echo json_encode($returnAry);
            exit(); 
          }         
    }

    public function scanDetail() {
        $Model = new \Think\Model(); // 实例化一个model对象 没有对应任何数据表
        // $id = 2545;
        $good = $Model->query("SELECT DISTINCT u.goodsid,u.shopid,a.*,COUNT(*) as total,max(created_at) as last_time FROM goods AS u LEFT JOIN scan_record a ON
 u.goodsid = a.goodsid WHERE  u.shopid = " . $_GET['shopid'] . " AND u.goodsid=a.goodsid GROUP BY a.goodsid;");
/*        $res = explode(',',$good[0]['images']);
        var_dump($res);die;*/

        foreach($good as $key => $value) {
            $res = explode(',', $value['images']);
            $good[$key]['images'] = $res[0];
            $goods[$key]['shopid'] = $_GET['shopid'];

        }
        $this->assign('good',$good);
        $this->display('ScanDetail.php');
    }

    public function scanUser() {
        $res = M('scan_record')->where('goodsid = ' . I('get.goodsid',''))->field('uid')->select();
        $uid = array();
        foreach($res as $key => $value) {
            $uid[$key] = $value['uid'];
        }
        $uid = implode(',',$uid);
        $user = M('user')->where('userid In ' . '(' . $uid . ')')->field('niname,images,phone,sex')->select();
        $user['shopid'] = $_GET['shopid'];
        // var_dump($user);die;
        $this->assign('user',$user);
        $this->display('ScanUser.php');
    }

    public function scanList(){
        $res = I('post.keywords','');

        $goods = M('scan_record')->distinct(true)->field('goodsid')->select();
        $goodsid = array();
        foreach ($goods as $key => $value) {
            $goodsid[$key] = $value['goodsid'];
        }
        $goodsid = implode(',',$goodsid);

        $shopId = M('goods')->distinct(true)->where('goodsid In ' . '(' . $goodsid . ')')->field('shopid')->select();
        $shopid = array();
        foreach($shopId as $key => $value) {
            $shopid[$key] = $value['shopid'];
        }
        $shopid = implode(',',$shopid);

        if(I('post.keywords')){
            $shop =M('shop')->where('shopid IN ' . '(' . $shopid . ')' . ' AND name LIKE ' . '"%' .I('post.keywords','') . '%"')->field('name,images,shopid')->select();
        }else{
            $shop = M('shop')->where('shopid IN ' . '(' . $shopid . ')')->field('name,images,shopid')->select();       
        }


        $this->assign('shop',$shop);
        $this->display("ScanList.php");
    }

    /**
     * [检索页]
     * @Author   Axing
     * @DateTime 2017-08-21
     * @return   [type]     [description]
     */
    public function search() {
        $this->display();
    }

}

